package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.MysqlBackups;
import com.example.buildtable.service.MysqlBackupsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "数据库备份")
@RequestMapping(value = "/mysqlBackups/")
public class MysqlBackupsController {
    @Autowired
    private MysqlBackupsService mysqlBackupsService;
    
    @Operation(summary = "查询所有数据库备份数据")
    @GetMapping(value = "/getAllMysqlBackups")
    public R getAllMysqlBackups() {
        List<MysqlBackups> mysqlBackupsList = mysqlBackupsService.list();
        return R.ok("查询所有数据库备份数据成功").data("mysqlBackupsList", mysqlBackupsList);
    }
    
    @Operation(summary = "注册数据库备份")
    @PostMapping(value = "/addMysqlBackups")
    public R addMysqlBackups(MysqlBackups mysqlBackups) {
        boolean flag = mysqlBackupsService.save(mysqlBackups);
        if (flag) {
            return R.ok("数据库备份成功");
        } else {
            return R.ok("数据库备份失败");
        }
    }

    @Operation(summary = "删除数据库备份")
    @GetMapping(value = "/deleteMysqlBackups/{backupsId}")
    public R deleteMysqlBackups(@PathVariable("backupsId") Integer backupsId) {
        boolean flag = mysqlBackupsService.removeById(backupsId);
        if (flag) {
            return R.ok("删除数据库备份成功");
        } else {
            return R.ok("删除数据库备份失败");
        }
    }

    @Operation(summary = "按照数据库备份查看")
    @GetMapping(value = "/getMysqlBackupsById/{backupsId}")
    public R getMysqlBackupsById(@PathVariable("backupsId") Integer backupsId) {
        MysqlBackups mysqlBackups = mysqlBackupsService.getById(backupsId);
        if (mysqlBackups != null) {
            return R.ok("按照数据库备份查看成功").data("mysqlBackups", mysqlBackups);
        } else {
            return R.error("按照数据库备份查看失败");
        }
    }

    @Operation(summary = "修改数据库备份")
    @PostMapping(value = "/updateMysqlBackups")
    public R updateMysqlBackups(MysqlBackups mysqlBackups) {
        boolean flag = mysqlBackupsService.updateById(mysqlBackups);
        if (flag) {
            return R.ok("修改数据库备份成功");
        } else {
            return R.ok("修改数据库备份失败");
        }
    }
}